package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.UserGetDTO;
import com.xbongbong.paas.pojo.vo.UserGetVO;
import com.xbongbong.paas.pojo.vo.UserMembersVO;
import com.xbongbong.paas.service.UserService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.permission.pojo.dto.MySetDTO;
import com.xbongbong.pro.permission.pojo.vo.MySetVO;
import com.xbongbong.pro.user.pojo.dto.UserInfoAuthDTO;
import com.xbongbong.pro.user.pojo.dto.UserMembersAddDTO;
import com.xbongbong.pro.user.pojo.dto.UserMembersGetDTO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 移动端 我的-设置
 * 创建时间 2019/5/6 11:26 AM
 * 修改时间 2019/5/6 11:26 AM
 * @author chy
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/user")
public class MobileUserController {

    @Resource
    private UserService userService;

    /**
     * 移动端 我的-设置 里面可设置项
     * @param mySetDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/mySet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String mySet(@RequestBody @Valid MySetDTO mySetDTO, BindingResult br){
        XbbResponse<MySetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MySetVO mySetVO = userService.mySet(mySetDTO);
                response = new XbbResponse<>(mySetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    
    /**
     * 获取用户信息(非登录用户的)
     *
     */
    @RequestMapping(value = "/info/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getUserInfo(@RequestBody @Valid UserGetDTO userGetDTO, BindingResult br) throws Exception {
        XbbResponse<UserGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                UserGetVO userGetVO = userService.getUserInfoByTargetUserId(userGetDTO);
                response = new XbbResponse<>(userGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 钉钉个人信息授权
     */
    @RequestMapping(value = "/dingtalk/auth", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String userInfoAuth(@RequestBody @Valid UserInfoAuthDTO userInfoAuthDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = userService.userInfoAuth(userInfoAuthDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response);
    }

    /**
     *
     * @Description: 添加常用团队成员
     * @param
     * @return: java.lang.String
     * @Author: shen.yang
     * @Date: 2022/1/24  10:20
     */
    @RequestMapping(value = "/addMembers", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addMembers(@RequestBody @Valid UserMembersAddDTO userMembersAddDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            BaseVO baseVO = userService.addMembers(userMembersAddDTO);
            response = new XbbResponse<>(baseVO);
        }
        return JSON.toJSONString(response);
    }

    /**
     *
     * @Description: 获取团队常用成员
     * @param userMembersGetDTO
     * @param br
     * @return: java.lang.String
     * @Author: shen.yang
     * @Date: 2022/1/25  9:32
     */
    @RequestMapping(value = "/getMembers", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getMembers(@RequestBody @Valid UserMembersGetDTO userMembersGetDTO, BindingResult br){
        XbbResponse<UserMembersVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                UserMembersVO members = userService.getMembers(userMembersGetDTO);
                response = new XbbResponse<>(members);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


}
